//
//  BasicUseModelWithMediaShareBBUI.m
//  MediaShareBB
//
//  Created by Artak Gevorgyan on 5/23/13.
//  Copyright (c) 2013 InnoriseEntertainment. All rights reserved.
//

#import "BasicUseModelWithMediaShareBBUI.h"
#import "MediaShareBB.h"
#import "MediaShareBBUtilityMethods.h"
#import "MediaShareCommunity.h"
#import "MediaShareData.h"
#import "FBMediaShareCommunity.h"
#import "MediaShareCommunityEntity.h"
#import "FBMediaShareCommunityEntity.h"

#import "SVProgressHUD.h"

@interface BasicUseModelWithMediaShareBBUI ()<MediaShareBBDelegate,
												UIActionSheetDelegate>
{
	MediaShareBB* _mediaShareBB;
}

-(void) setupMediaShareBB;
-(NSString*) getMediaTypeNameOfData:(MediaShareData*) data;

@end

@implementation BasicUseModelWithMediaShareBBUI

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }
    return self;
}

#pragma mark - View life cycle

- (void)viewDidLoad
{
	self.view.backgroundColor = [UIColor lightGrayColor];
	[self setupMediaShareBB];
	[self useModel];
//	UIButton* b = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//	b.frame = CGRectMake(10, 10, 40, 20);
//	[b addTarget:self action:@selector(buttonClicked:)
//		forControlEvents:UIControlEventTouchUpInside];
//	[self.view addSubview:b];
}

-(void) buttonClicked:(id) sender {
	[self useModel];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void) viewDidAppear:(BOOL)animated {
}


#pragma mark - Private methods

-(void) setupMediaShareBB {
	_mediaShareBB = [[MediaShareBB alloc] initWithAllCommunitiesSupport];
	_mediaShareBB.delegate = self;
	[self.view addSubview:_mediaShareBB.view];
	[_mediaShareBB.view sendSubviewToBack:self.view];
}

-(void) useModel {
	// Assuming that per user request a sharing view must be presented
	[self presentSharingView];
}

-(void) presentSharingView {
	if (IPHONE5 || IPHONE) {
		[self presentActionSheet];
	} else if (IPAD) {
		//		[self presentPopover];
	}
}

-(void) presentActionSheet {
	UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:@"Share via"
															 delegate:self
													cancelButtonTitle:nil
											   destructiveButtonTitle:nil
													otherButtonTitles:nil];
	for (MediaShareCommunity* cm in _mediaShareBB.configuredCommunities) {
		[actionSheet addButtonWithTitle:cm.name];
	}
    [actionSheet showInView:self.view];
}

-(NSString*) getMediaTypeNameOfData:(MediaShareData*) data {
	NSString* s = @"";
	switch (data.mediaType) {
		case IMAGE_MEDIA_TYPE: {
			s = @"image";
			break;
		} case VIDEO_MEDIA_TYPE: {
			s = @"video";
			break;
		} case URL_MEDIA_TYPE: {
			s = @"url";
			break;
		} default:
			break;
	}
	return s;
}

#pragma mark - UIActionSheetDelegate methods

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
	NSLog(@"button index = %d", buttonIndex);
	NSArray* c = _mediaShareBB.configuredCommunities;
	MediaShareCommunity* comm = [c objectAtIndex:buttonIndex];
	[_mediaShareBB setSelectedCommunityWithType:comm.communityType];
	NSLog(@"Selected community_:%@", comm.description);

	[comm login];
}

#pragma mark - MediaShareBB delegation

-(void) canceled {
}

// This is where the developer should present the users of the community
-(void) mediaShareCommunitySelected:(MediaShareCommunity*) mediaShareCommunity {
	
	// List all the entitites registered in this community
//	for(MediaShareCommunityEntity* entity in mediaShareCommunity.entities) {
//		NSLog(@"Entity ID: %@", [entity entityId]);
//		if([mediaShareCommunity isKindOfClass:[FBMediaShareCommunity class]]) {
////			FBMediaShareCommunity* fbComm = (FBMediaShareCommunity*) mediaShareCommunity;
//		}
//	}
	UIImage* image = [UIImage imageNamed:@"example.png"];
	NSString* name = [NSString stringWithFormat:@"mediaShareBB_%@",
					  [MediaShareBBUtilityMethods getCurrentData]];
	MediaShareData* data = mediaShareCommunity.communityType != MEDIA_SHARE_COMMUNITY_YOUTUBE
							? [[MediaShareData alloc] initWithImage:image andName:name]
							: [[MediaShareData alloc] initWithVideoAtPath:@"sample_video"
																  andName:@"sample_video"];
	[mediaShareCommunity shareMedia:data];
}

-(void) willShareMedia:(MediaShareData*) media through:(MediaShareCommunity*) comm {
	NSString* type = [self getMediaTypeNameOfData:media];
	NSString* a = (comm.communityType == MEDIA_SHARE_COMMUNITY_SAVE_TO_PHOTOS) ? @"Saving"
																			: @"Uploading";
	NSString* s = [NSString stringWithFormat:@"%@ %@: %@", a, type, media.fileName];
	[SVProgressHUD showWithStatus:s];
}

-(void) updateProgress:(CGFloat) progress forCommunity:(MediaShareCommunity*) comm {

}

-(void) didFinishSharingMedia:(MediaShareData*) media through:(MediaShareCommunity*) comm {
	NSString* s = (comm.communityType == MEDIA_SHARE_COMMUNITY_SAVE_TO_PHOTOS) ? @"Saved"
																			: @"Uploaded";
	[SVProgressHUD showSuccessWithStatus:s];
}

-(void) didFailedSharingMedia:(MediaShareData*) media through:(MediaShareCommunity*) comm
					withError:(NSError*) error {
	NSString* s = [NSString stringWithFormat:@"Upload failed with error: %@", error];
	[SVProgressHUD showErrorWithStatus:s];
}

#pragma mark - 

-(void)documentInteractionController:(UIDocumentInteractionController *)controller
	   willBeginSendingToApplication:(NSString *)application {
	
}

@end
